const app = getApp();
const reuqestHead = app.globalData.imageRequest;
const commRequest = require("../../request/commRequest.js");
const encryption=require("../../request/encryptionUtils.js");
Page({
  data: {
    formats: {},
    readOnly: false,
    placeholder: '',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,
    reuqestHead:reuqestHead,
    goodInfo:{}
  },
  readOnlyChange() {
    this.setData({
      readOnly: !this.data.readOnly
    })
  },
  onLoad(e) {
     //获取商品信息
     this.getGoodById(e);
    const platform = wx.getSystemInfoSync().platform
    const isIOS = platform === 'ios'
    this.setData({ isIOS})
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      if (res.height === keyboardHeight) return
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            that.updatePosition(keyboardHeight)
            that.editorCtx.scrollIntoView()
          }
        })
      }, duration)

    })
   
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 50
    const { windowHeight, platform } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    this.setData({ editorHeight, keyboardHeight })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const { statusBarHeight, platform } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
 async onEditorReady() {
    const that = this
    const query = wx.createSelectorQuery()
    query.select('#editor').context(function (res) {
      that.editorCtx = res.context
       console.log(that.data.placeholder);
      res.context.setContents({
        html: that.data.placeholder,
        success: function (res) {
          // console.log(res.data)
        },
        fail: function (error) {
          console.log(error)
        }
      })
    }).exec()
  
  },
  blur() {
    this.editorCtx.blur()
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({ formats })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
 // 点击图片将图片插入富文本编辑器里面
  insertImage() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res.tempFilePaths,'上传图片')
        wx.uploadFile({
          url: 'http://2619p587h5.qicp.vip/wx/goodsList/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
         header:{
           'Authorization': wx.getStorageSync('token')
         },
          success: function (res) {
            console.log(res.data,'图片上传之后的数据')
            var data = JSON.parse(res.data)
            console.log(data.data)
            that.editorCtx.insertImage({
              src: that.data.reuqestHead+"/"+data.data,
              success: function () {
                console.log('insert image success')
              }
            })
          }
        }) 
      }
    })
  },
  /**
   * 查询数据
   */
  async getGoodById(e){
    let result=await commRequest.postRequestHeader({url:'/goodsList/getGoodById',data:{'id':e.goodId}});
    if(result.data.code==0){
      this.setData({
        placeholder:result.data.data.goodsDetails+"<p> </p>",
        goodInfo:result.data.data
      })
    }
  },
  /**
   * 获取富文本编辑器中的值
   */
  editorBlur(e){
    this.setData({
      placeholder:e.detail.html
    })
  },
  /**
   * 提交修改内容
   */
 async updateGoodInfo(e){
    console.log(e.detail.value.goodName);
    let data={goodsName:e.detail.value.goodName,goodsMoneyNumber:e.detail.value.money,goodsDetails:this.data.placeholder};
    await commRequest.postRequestHeader({url:'/goodsList/updateGoodInfo',dat:data})
  }
})
 